// +----------------------------------------------------------------------
// | Manong.Cloud [ 领酷码农云 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.manong.cloud All rights reserved.
// +----------------------------------------------------------------------
// | Author: 稻草人 <qinuoyun@qq.com>
// +----------------------------------------------------------------------
// | Member: 围棋 飘逸者 Loumt Silence24
// +----------------------------------------------------------------------

import BaseBusinessController from './../base/BaseBusinessController'

class DemoController extends BaseBusinessController {


  async actionIndex() {
    let building_id = 8;
    let building = await this.modules("building").where({id: building_id}).select();
    let drive = await this.modules("building.LockDoor").where({building_id: building_id}).select();
    //读取该楼宇已经认证的用户
    let sql = `SELECT
	hz_company.building_id, 
	hz_user.id, 
	hz_user.real_name, 
	hz_user.id_card, 
	hz_user.company_id, 
	hz_user.head, 
	hz_user.certification
FROM
	hz_company
	INNER JOIN
	hz_user
	ON 
		hz_company.id = hz_user.company_id
WHERE
	hz_company.building_id = :building_id AND
	hz_user.certification = 1`;
    let data = await db.sql(sql, {building_id: building_id});

    let userArray = [];
    data.forEach(item => {
      console.log("item", item)
      let dataInfo = this.setUserInfo(item.id, item.real_name, item.id_card, item.head);
      userArray.push(dataInfo);
    })

    console.log("userArray", userArray)

    return userArray
  }

  /**
   * 设置用户信息
   * @param id
   * @param name
   * @param idno
   * @returns {{cards: *[], elevator: string, effect: number, expire: number, name, rules: *[], state: number, userid: string, idno: string, desc: string}}
   */
  setUserInfo(id, name, idno, head) {
    let timestamp = Math.round(new Date().getTime() / 1000).toString();
    return {
      "userid": "qn2022_" + id,     // 用户ID，全局唯一
      "name": name,     // 必填：用户姓名，防疫产品白名单功能时必须与idno匹配
      "desc": "",   // 可选：用户部门/住宅门牌号等，可不填写，平台可以发用户最近的组织名
      "idno": String(idno), //可选：个人身份证，在防疫产品中，白名单功能会用此实现健康码查询
      "effect": Number(timestamp),       // 用户生效时间，不填则默认为0，部分情况设备掉电丢失时间，此值填写有核验失败风险
      "expire": -1,       // 用户过期时间，不填则当前时刻+1年，建议填写一个较大值如果不用的话，-1为不限制过期
      "state": 1,                 // 用户状态，正常用户是正值，负值为黑名单，定制产品使用，可不填写
      "cards": [],
      "rules": [],
      "elevator": "0000000001000000",
      "face": {
        "feature": head
      }
    }
  }

}

export default DemoController;
